package com.marvels.product.param.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.MarvelsUtils;
import com.marvels.product.param.dto.McptManagementFeeDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.MngFeeService;

/**
 * 管理费
 * @author 宁华勇
 *
 */
@RestController
@RequestMapping("/product/param/mngfee/service")
public class MngFeeServiceController {
	
	@Autowired
	private MngFeeService mngFeeService;
	
	@RequestMapping("list")
	public MarvelsResultDTO list(@RequestBody Map<String,Object> param){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(param));
		
		PageInfo<McptManagementFeeDTO> pageInfo = null;
		
		try {
			pageInfo = mngFeeService.getPageInfoByParam(param);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageInfo);
	}
	
	@RequestMapping("getKey/{managementFeeId}")
	public MarvelsResultDTO edit(@PathVariable Long managementFeeId,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(managementFeeId));
		McptManagementFeeDTO entity = null;
		try {
			 entity = mngFeeService.getEntityByPrimaryKey(managementFeeId);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), entity);
	}
	
	
	@RequestMapping("save")
	public MarvelsResultDTO save(@RequestBody McptManagementFeeDTO dto,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setCreatedBy(userName);
			dto.setManagementFeeCode(MarvelsUtils.generateUUIDCode("mngfee"));
			mngFeeService.insert(dto);
		}  catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), dto);
	}
	
	@RequestMapping("update")
	public MarvelsResultDTO update(@RequestBody McptManagementFeeDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			UserDTO user = CacheUtils.getCacheUser();
			String userName = user.getUserName();
			dto.setUpdatedBy(userName);
			mngFeeService.updateByPrimaryKey(dto);
		}   catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}  catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
	@RequestMapping("updateStatus")
	public MarvelsResultDTO updateStatus(@RequestBody McptManagementFeeDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			mngFeeService.updateStatus(dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("管理费审核异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
	@RequestMapping("selectByProductInfo")
	@ResponseBody
	public McptManagementFeeDTO selectByProductInfo(@RequestBody McptProduct product){
		return mngFeeService.selectByProductInfo(product);
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public MarvelsResultDTO deleteByPrimaryKey(@RequestBody McptManagementFeeDTO manFee){
		try {
			int count = mngFeeService.deleteByPrimaryKey(manFee);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("管理费删除异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统删除异常", e);;
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
}
